package in.muton.edushare.pages.cp.config;

import java.util.ArrayList;

import in.muton.edushare.pages.cp.CPBase;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ConfigPropertyName;
import com.edushare.common.dao.ConfigPropertiesDAO;
import com.edushare.common.vo.ConfigProperties;
import com.edushare.common.vo.Universities;

public class ConfigCP extends CPBase implements IHeaderContributor{

	private CayenneModel<ConfigProperties> smtpHostProp;
	private CayenneModel<ConfigProperties> smtpPortProp;
	private CayenneModel<ConfigProperties> mailIdProp;
	private CayenneModel<ConfigProperties> mailPasswordProp;
	private CayenneModel<ConfigProperties> uploadDir;
	private CayenneModel<ConfigProperties> uploadLtProp;
	private CayenneModel<ConfigProperties> bkpFileSize;
	private CayenneModel<ConfigProperties> glassfishDomain;
	private CayenneModel<ConfigProperties> dbName;
	private CayenneModel<ConfigProperties> dbUser;
	private CayenneModel<ConfigProperties> dbPassword;
	private CayenneModel<ConfigProperties> dbHost;
	private CayenneModel<ConfigProperties> dbPort;
	private Button saveTop;
	private Button saveBottom;
	private Form<Void> form;
	private ArrayList<Boolean> boolChoices;
	private CayenneModel<ConfigProperties> mailStarttlsProp;
	private CayenneModel<ConfigProperties> mailUseSSLProp;
	private CayenneModel<ConfigProperties> mailUseAuthProp;
	
	public ConfigCP(PageParameters params) {
		super(params);
	}

	public ConfigCP(IModel<Universities> univModel){
		super(univModel,3);
		initPage();
	}

	private void initPage() {
		
		loadDataFromDB();
		boolChoices = new ArrayList<Boolean>();
		boolChoices.add(new Boolean(true));
		boolChoices.add(new Boolean(false));
		form = new Form<Void>("form"){
			private static final long serialVersionUID = 1194457036783995375L;

			@Override
			protected void onSubmit() {
				getSessionORMInterface().commitAllChanges();
			}
		};
		add(form);
		TextField<String> smtpHost = 
			new TextField<String>("smtpHost", new PropertyModel<String>(ConfigCP.this.smtpHostProp, "value"));
		form.add(smtpHost);
		TextField<String> smtpPort = 
			new TextField<String>("smtpPort", new PropertyModel<String>(ConfigCP.this.smtpPortProp, "value"));
		form.add(smtpPort);
		TextField<String> mailId = 
			new TextField<String>("mailId", new PropertyModel<String>(ConfigCP.this.mailIdProp, "value"));
		form.add(mailId);
		PasswordTextField mailPassword = 
			new PasswordTextField("mailPassword", new PropertyModel<String>(ConfigCP.this.mailPasswordProp, "value"));
		mailPassword.setResetPassword(false);
		form.add(mailPassword);
		
		//drop down options
		DropDownChoice<Boolean> starttls = new DropDownChoice<Boolean>("starttls", boolChoices);
		starttls.setModel(new PropertyModel<Boolean>(this, "startTlsBool"));
		form.add(starttls);
		
		DropDownChoice<Boolean> useSSL = new DropDownChoice<Boolean>("useSSL", boolChoices);
		useSSL.setModel(new PropertyModel<Boolean>(this, "useSSLBool"));
		form.add(useSSL);
		
		DropDownChoice<Boolean> useAuth = new DropDownChoice<Boolean>("useAuth", boolChoices);
		useAuth.setModel(new PropertyModel<Boolean>(this, "useAuthBool"));
		form.add(useAuth);
		
		TextField<String> storageDir = 
			new TextField<String>("storageDir", new PropertyModel<String>(ConfigCP.this.uploadDir, "value"));
		form.add(storageDir);
		TextField<String> uploadLt = 
			new TextField<String>("uploadLt", new PropertyModel<String>(ConfigCP.this.uploadLtProp, "value"));
		form.add(uploadLt);
		TextField<String> bkpFileSize = 
			new TextField<String>("bkpFileSize", new PropertyModel<String>(ConfigCP.this.bkpFileSize, "value"));
		form.add(bkpFileSize);
		TextField<String> glassfishDomain = 
			new TextField<String>("glassfishDomain", new PropertyModel<String>(ConfigCP.this.glassfishDomain, "value"));
		form.add(glassfishDomain);
		TextField<String> dbName = 
			new TextField<String>("dbName", new PropertyModel<String>(ConfigCP.this.dbName, "value"));
		form.add(dbName);
		TextField<String> dbUser = 
			new TextField<String>("dbUser", new PropertyModel<String>(ConfigCP.this.dbUser, "value"));
		form.add(dbUser);
		TextField<String> dbPassword = 
			new TextField<String>("dbPassword", new PropertyModel<String>(ConfigCP.this.dbPassword, "value"));
		form.add(dbPassword);
		TextField<String> dbHost = 
			new TextField<String>("dbHost", new PropertyModel<String>(ConfigCP.this.dbHost, "value"));
		form.add(dbHost);
		TextField<String> dbPort = 
			new TextField<String>("dbPort", new PropertyModel<String>(ConfigCP.this.dbPort, "value"));
		form.add(dbPort);
		form.add(saveTop = new Button("saveTop"));
		form.add(saveBottom = new Button("saveBottom"));
		form.add(new Button("undoTop"){
			private static final long serialVersionUID = 2308589698993126240L;

			@Override
			public void onSubmit() {
				loadDataFromDB();
				form.clearInput();
			}
		}.setDefaultFormProcessing(false));
		form.add(new Button("undoBottom"){
			private static final long serialVersionUID = -2234294598241640334L;

			@Override
			public void onSubmit() {
				loadDataFromDB();
				form.clearInput();
			}
		}.setDefaultFormProcessing(false));
	}

	private void loadDataFromDB() {
		this.smtpHostProp 		= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_HOST));
		this.smtpPortProp 		= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_PORT));
		this.mailIdProp 		= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_NOREPLY_USER));
		this.mailPasswordProp 	= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_NOREPLY_PASSWORD));
		this.mailStarttlsProp 	= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_STARTTLS));
		this.mailUseSSLProp 	= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_AUTH));
		this.mailUseAuthProp 	= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.MAIL_USING_SSL));
		
		this.uploadDir 			= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.STORAGE_ROOT_DIRECTORY));
		this.uploadLtProp 		= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.UPLOAD_MAXSIZE_KB));
		this.bkpFileSize 		= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.SYSTEM_BACKUP_FILE_SIZE_IN_MB));
		
		this.glassfishDomain 	= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.SYSTEM_GLASSFISH_DOMAIN));
		this.dbName 			= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_NAME));
		this.dbUser 			= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_USER));
		this.dbPassword 		= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_PASSWORD));
		this.dbHost 			= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_HOST));
		this.dbPort 			= new CayenneModel<ConfigProperties>(new ConfigPropertiesDAO(getSessionORMInterface()).getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_PORT));
		
	}
	
	public Boolean getStartTlsBool(){
		return Boolean.parseBoolean(mailStarttlsProp.getObject().getValue());
	}
	
	public void setStartTlsBool(Boolean bool){
		mailStarttlsProp.getObject().setValue(bool.toString());
	}
	
	public Boolean getUseAuthBool(){
		return Boolean.parseBoolean(mailUseAuthProp.getObject().getValue());
	}
	
	public void setUseAuthBool(Boolean bool){
		mailUseAuthProp.getObject().setValue(bool.toString());
	}
	
	public Boolean getUseSSLBool(){
		return Boolean.parseBoolean(mailUseSSLProp.getObject().getValue());
	}
	
	public void setUseSSLBool(Boolean bool){
		mailUseSSLProp.getObject().setValue(bool.toString());
	}
	
	@Override
	public void renderHead(IHeaderResponse response) {
		response.renderOnDomReadyJavascript("" +
				"$('#alertSave').dialog({"+
			    "autoOpen: false,"+
			    "width: 600,"+
			    "modal: true,"+
			    "buttons: {"+
			        "\"Ok\": function() {"+ 
			        "$(this).dialog(\"close\");"+
			        "$('#"+form.getMarkupId()+"').submit();"+
			         "return true; "+
			        "}, "+
			        "\"Cancel\": function() {"+ 
				    "$(this).dialog(\"close\");"+ 
			            "return false; "+
			        "} "+
			    "}"+
			"});"+
			"$('#"+saveTop.getMarkupId()+"').click(function(){$('#alertSave').dialog('open');return false;});"+
			"$('#"+saveBottom.getMarkupId()+"').click(function(){$('#alertSave').dialog('open');return false;});"+
				"");
	}
}
